Overwriting hello.txt
A file is a collection of data stored on a secondary storage device like hard disk.
Absolute Path contains root while Relative path needs to be combined with another path to access a file
A text file is a stream of characters that can be sequentially processed by a computer in forward direction. For this reason a text file is usually opened for only one kind of operation (reading, writing, or appending) at any given time.
A binary file is a file which may contain any type of data, encoded in binary form for computer storage and processing purposes.
image.png
write() method writes any string to an open file.write() method does not add a newline character ('\n') to the end of the string.writelines() method takes a list and write each entry in its own line (if it has the “” character at the end).Hello World,
Welcome to the world of Python
Enjoy Learning Python
Variable Type Data/Info
-------------------------------------
file TextIOWrapper <_io.TextIOWrapper name='<...>de='w' encoding='cp1252'>
i str Python is a very simple yet powerful language
line str this is a python text file.\n
lines list n=3
os module <module 'os' (frozen)>
t TextIOWrapper <_io.TextIOWrapper name='<...>de='r' encoding='cp1252'>
words list n=6
fileObj.read([count])
- The read() method starts reading from the beginning of the file and if count is missing or has a negative value then, it reads the entire contents of the file (i.e., till the end of file). - The readlines() method is used to read all the lines in the file
WITH KEYWORDWITH KEYWORDWITH KEYWORD['Hello', 'World,', 'Welcome', 'to', 'the', 'world', 'of', 'Python', 'Enjoy', 'Learning', 'Python']
With every file, the file management system associates a pointer often known as file pointer that facilitate the movement across the file for reading and/ or writing data.
Python has various methods that tells or sets the position of the file pointer.
For example, the tell() method tells the current position within the file at which the next read or write operation will occur. It is specified as number of bytes from the beginning of the file.
The seek(offset[, from]) method is used to set the position of the file pointer or in simpler terms, move the file pointer to a new location. The offset argument indicates the number of bytes to be moved and the from argument specifies the reference position from where the bytes are to be moved.
file = open("hello1.txt","rb")
print("Position of file pointer before reading is - ", file.tell())
print (file.read(10))
print("Position of file pointer after reading is - ", file.tell())
print("Setting 3 bytes from the current position of file pointer")
file.seek(3, 1)
print(file.read())
file.close() Position of file pointer before reading is - 0
b'Hello Worl'
Position of file pointer after reading is - 10
Setting 3 bytes from the current position of file pointer
b'Welcome to the world of Python Enjoy Learning Python\r\n Python is a very simple yet powerful language'
| Methods and functions | Description |
|---|---|
| open() | Returns a file object and is most commonly used with two arguments: open(filename, mode) |
| file.close() | Close the file. |
| file.read([size]) | Read the entire file. If size is specified then read at most size bytes. |
| file.readline([size]) | Read one line from the file. If size is specified then read at most size bytes. |
| file.readlines([size]) | Read all the lines from the file. If size is specified then read at most size bytes. |
| file.tell() | Returns file object’s current position in the file. |
| file.seek(int) | Changes the file object’s current position to the given int. |
| file.write(string) | Writes the contents of string to the file. |
| file.writelines(string) | Writes the list of contents of string to the file. |
Manish Patel